#include "stdafx.h"
#include "QSysButton.h"

#include <QtWidgets\QWidget>
#include <QEvent>
#include <QMouseEvent>
#include <QPaintEvent>
#include <QPainter>
#include <QBitmap>


QSysButton::QSysButton( QWidget *parent /*= 0*/ )
{
	status = NORMAL;
	mouse_press = false;
}

QSysButton::QSysButton( QString picName,QString tipText,QWidget *parent /*= 0*/ )
{
	pixmap.load(picName);
	setWindowOpacity(0);
	setFlat(true);
	btn_width=pixmap.width()/4;
	btn_height=pixmap.height();
	setFixedSize(btn_width,btn_height);
	setToolTip(tipText);

	status=NORMAL;
	mouse_press = false;
}

QSysButton::~QSysButton()
{

}

void QSysButton::loadPixmap( QString pic_name )
{
	pixmap.load(pic_name);
	btn_width = pixmap.width()/4;
	btn_height = pixmap.height();
	setFixedSize(btn_width, btn_height);
}

void QSysButton::enterEvent( QEvent * )
{
	status = ENTER;
	update();
}

void QSysButton::leaveEvent( QEvent * )
{
	status = NORMAL;
	update();
}

void QSysButton::mousePressEvent( QMouseEvent *event )
{
	if(event->button() == Qt::LeftButton)
	{
		mouse_press = true;
		status = PRESS;
		update();
	}
}

void QSysButton::mouseReleaseEvent( QMouseEvent *event )
{
	if(mouse_press)
	{
		mouse_press = false;
		status = ENTER;
		update();
		emit clicked();
	}
}

void QSysButton::paintEvent( QPaintEvent * )
{
	QPainter painter(this);
	painter.drawPixmap(rect(), pixmap.copy(btn_width * status, 0, btn_width, btn_height));
}
